元件學了一些後,我們來學習如何跳轉Activity,我們如何跳轉Activity該怎麼做呢?
其實很簡單~ 就是我們要學習的Intent,我們需要運用它轉換Activity,然後我會做個簡單的今日運氣抽取的小範例來做~
因為要跳轉Activity,我們除了原本的MainActivity外,還需要新增一個Activity。
於自己的com.example.(名稱)右鍵>New>Activity> 點擊Empty Views Activity
命名第二個Activity,建議第一個英文字母為大寫,同時layout也會變更命名
點擊Finish就成功了
主頁面設計,設計出自己喜歡的樣子~(也可以跟我做一樣)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.2" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.8" />
<Button
android:id="@+id/main_touch_btn"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/my_button"
android:text="抽取運氣"
android:textSize="28dp"
app:backgroundTint="@null"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<EditText
android:id="@+id/main_name_et"
android:layout_width="0dp"
android:layout_height="0dp"
android:ems="10"
android:hint="Name"
android:inputType="text"
app:layout_constraintBottom_toTopOf="@+id/guideline5"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/guideline8" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25" />
<TextView
android:id="@+id/main_theme_tv"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="今日運氣"
android:textSize="30dp"
android:textStyle="bold"
android:textColor="#8F56E4"
android:background="#C599F4"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SettingActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.05" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.35" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.9" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" />
<Button
android:id="@+id/setting_back_btn"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="返回"
android:textSize="28dp"
android:background="@drawable/my_button"
app:backgroundTint="@null"
app:layout_constraintBottom_toTopOf="@+id/guideline14"
app:layout_constraintEnd_toStartOf="@+id/guideline11"
app:layout_constraintStart_toStartOf="@+id/guideline10"
app:layout_constraintTop_toTopOf="@+id/guideline13" />
<ImageView
android:id="@+id/setting_img_img"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline9"
app:layout_constraintEnd_toStartOf="@+id/guideline11"
app:layout_constraintStart_toStartOf="@+id/guideline10"
app:layout_constraintTop_toTopOf="@+id/guideline6"
app:srcCompat="@drawable/img_" />
<TextView
android:id="@+id/setting_result_tv"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="TextView"
android:textSize="30dp"
android:textStyle="bold"
android:gravity="center"
app:layout_constraintBottom_toTopOf="@+id/guideline12"
app:layout_constraintEnd_toStartOf="@+id/guideline11"
app:layout_constraintStart_toStartOf="@+id/guideline10"
app:layout_constraintTop_toTopOf="@+id/guideline9" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.ittext;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
//宣告
private Button myButton;
private EditText nameEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
//綁定
myButton = findViewById(R.id.main_touch_btn);
nameEditText = findViewById(R.id.main_name_et);
//點擊事件(監聽器)
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = nameEditText.getText().toString();
Intent intent = new Intent(MainActivity.this, SettingActivity.class);
//指定名稱字串,可用標籤取得資料
intent.putExtra("NAME",name);//將bmi放進Intent
startActivity(intent);//將intent 發送到Android,判別後顯示到SA畫面中
}
});
}
}
程式碼 | 說明 |
---|---|
getFloatExtra | 取得float型態資料 |
getIntExtra | 取得int型態資料 |
getLongExtra | 取得long型態資料 |
getBooleanExtra | 取得boolean型態資料 |
getCharExtra | 取得char型態資料 |
getStringExtra | 取得String型態資料 |
getStringArrayExtra | 取得String陣列型態資料 |
getStringArrayListExtra | 取得內含String的List集合資料 |
大致有這些常用取得資料的方法,如果不太明白怎麼使用,其實打get後最會出現較詳細的選項跟甚麼型態資料(如下圖)
package com.example.ittext;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.util.Random;
public class SettingActivity extends AppCompatActivity {
//宣告
private Button backButton;
private TextView resultTextView;
//運氣結果
private String[] ans={"大吉","中吉","小吉","末吉","凶","大凶"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_setting);
//綁定
backButton = findViewById(R.id.setting_back_btn);
resultTextView = findViewById(R.id.setting_result_tv);
//呼叫Activity,取得intent
Intent intent = getIntent();
String name = intent.getStringExtra("NAME");
//get型態Extra 取得資料(資料標籤)
resultTextView.setText(name+",今天日氣是...\n"+ans[new Random().nextInt(6)]+"!");
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
恭喜你成功學會如何跳轉Activity,很簡單 對吧? 應該是吧
不會多做就會了,接下來就繼續學習更多元件吧~